﻿using System.Net;

namespace Blazor.CoffeeTalesApi.Entity;

public class IpKey(IPAddress ip) : IWhitelistKey
{
    private readonly IPAddress _ip = ip;

    public IPAddress IP => _ip;

    public bool Equals(IWhitelistKey? other)
    {
        if (other is null)
        {
            return false;
        }

        return other is IpKey key && Equals(key);
    }

    public string GetJsonRepresent() => $"ip:{_ip}";

    public override string ToString() => _ip.ToString();

    protected bool Equals(IpKey other)
    {
        return _ip.Equals(other._ip);
    }

    public override bool Equals(object? obj)
    {
        if (obj is null) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != GetType()) return false;
        return Equals((IpKey)obj);
    }

    public override int GetHashCode()
    {
        return _ip.GetHashCode();
    }
}